package com.zmn.plat.dubbo.impl.plat;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.common.dto.plat.servcategory.PlatServCategoryDRO;
import com.zmn.plat.common.dto.servcategory.ServCategoryDRO;
import com.zmn.plat.model.entity.plat.servcategory.PlatServCategory;
import com.zmn.plat.model.vo.plat.servcategory.PlatServCategoryVO;
import org.apache.dubbo.config.annotation.Service;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.dubbo.interfaces.plat.PlatServCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.services.interfaces.plat.servcategory.PlatServCategoryService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @author tanghongjun
 * @version v1.0
 * @since 2020/5/18
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class PlatServCategoryListRemoteServiceImpl implements PlatServCategoryListRemoteService {

    @Resource
    PlatServCategoryService platServCategoryService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<Boolean> checkOpenCategByPlatAndCateg(Integer plat, Integer servCategId, Integer showCategTwoId) {
        log.info("checkOpenCategByPlatAndCateg：plat={},servCategId={},showCategTwoId={}", plat, servCategId, showCategTwoId);

        if (Objects.isNull(plat) || Objects.isNull(servCategId) || Objects.isNull(showCategTwoId)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        int count = platServCategoryService.countOpenCategByPlatAndCateg(plat, servCategId, showCategTwoId);
        return ResponseDTO.success(count > 0);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PlatServCategoryDRO>> listByPlat(@NotNull(message = "plat不能为空") @Positive(message = "plat必须为大于0的正整数") Integer plat) {
        log.info("listByPlat：plat={}", plat);

        if (Objects.isNull(plat)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        List<PlatServCategoryVO> data = platServCategoryService.listVoByPlat(plat);
        List<PlatServCategoryDRO> items = data.stream().map(item -> {
            PlatServCategoryDRO platServCategoryDRO = new PlatServCategoryDRO();
            BeanUtils.copyProperties(item, platServCategoryDRO);
            return platServCategoryDRO;
        }).collect(Collectors.toList());

        return ResponseDTO.success(items, GlobalConsts.SUCCESS_MSG);
    }
}
